package utils.com;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Timer;
import java.util.TimerTask;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import missPuff.com.MissPuffActivity;
import missPuff.com.R;
import missPuff.com.VideoTabActivity;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;
import database.com.DatabaseHelper;

public class XMLtoDatabaseUtils {
	private Context context;
	private DatabaseHelper databaseHelper;
	private SQLiteDatabase sqliteDatabase;
	
	public XMLtoDatabaseUtils(Context context) {
		super();
		this.context = context;
		databaseHelper = new DatabaseHelper(context, "resource");
		sqliteDatabase = databaseHelper.getWritableDatabase();
	}


	public void resourcesParseToDB() {
		
		String resources_url =context.getString(R.string.resources_url);
		
		try {
			URL url = new URL(resources_url);
			URLConnection conn = url.openConnection();
			InputStream inputStream = conn.getInputStream();
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(inputStream);
			NodeList nodeList = document.getElementsByTagName("resource");
			sqliteDatabase.delete("video", null, null);

			for (int i = 0; i < nodeList.getLength(); i++) {
				ContentValues values = new ContentValues();
				String video_id = document.getElementsByTagName("video_id").item(i)
						.getFirstChild().getNodeValue();
				String video_title = document.getElementsByTagName("video_title").item(i)
						.getFirstChild().getNodeValue();
				String video_name = document.getElementsByTagName("video_name").item(i)
						.getFirstChild().getNodeValue();
				int video_size = Integer.parseInt(document.getElementsByTagName("video_size")
						.item(i).getFirstChild().getNodeValue());
				String video_playUrl = document.getElementsByTagName("video_playUrl").item(i)
						.getFirstChild().getNodeValue();
				String video_downloadUrl = document.getElementsByTagName("video_downloadUrl").item(i)
						.getFirstChild().getNodeValue();
				String video_time = document.getElementsByTagName("video_time").item(i)
						.getFirstChild().getNodeValue();
				String video_info = document.getElementsByTagName("video_info").item(i)
						.getFirstChild().getNodeValue();
				String video_sort = document.getElementsByTagName("video_sort").item(i)
						.getFirstChild().getNodeValue();
				String image_url = document.getElementsByTagName("image_url").item(i)
						.getFirstChild().getNodeValue();
				String image_name = document.getElementsByTagName("image_name").item(i)
						.getFirstChild().getNodeValue();
				int image_size = Integer.parseInt(document.getElementsByTagName("image_size")
						.item(i).getFirstChild().getNodeValue());
				
				values.put("video_id", video_id);
				values.put("video_title", video_title);
				values.put("video_name", video_name);
				values.put("video_size", video_size);
				values.put("video_playUrl", video_playUrl);
				values.put("video_downloadUrl", video_downloadUrl);
				values.put("video_time", video_time);
				values.put("video_info", video_info);
				values.put("video_sort", video_sort);
				values.put("image_url", image_url);
				values.put("image_name", image_name);
				values.put("image_size", image_size);
				values.put("video_localUrl", getVideoFolderPath() + video_name);
				values.put("image_localUrl", getImageFolderPath() + image_name);
				sqliteDatabase.insert("video", null, values);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void seasonParseToDB() {
		
		String resources_url =context.getString(R.string.seasons_url);
		
		try {
			URL url = new URL(resources_url);
			URLConnection conn = url.openConnection();
			InputStream inputStream = conn.getInputStream();
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(inputStream);
			NodeList nodeList = document.getElementsByTagName("season");
			sqliteDatabase.delete("season", null, null);

			for (int i = 0; i < nodeList.getLength(); i++) {
				ContentValues values = new ContentValues();
				String season_id = document.getElementsByTagName("season_id").item(i)
						.getFirstChild().getNodeValue();
				String season_title = document.getElementsByTagName("season_title").item(i)
						.getFirstChild().getNodeValue();
				int season_count = Integer.parseInt(document.getElementsByTagName("season_count")
						.item(i).getFirstChild().getNodeValue());
				String season_info = document.getElementsByTagName("season_info").item(i)
						.getFirstChild().getNodeValue();
				String pic_url = document.getElementsByTagName("pic_url").item(i)
						.getFirstChild().getNodeValue();
				String pic_name = document.getElementsByTagName("pic_name").item(i)
						.getFirstChild().getNodeValue();
				int pic_size = Integer.parseInt(document.getElementsByTagName("pic_size")
						.item(i).getFirstChild().getNodeValue());
				String image_url = document.getElementsByTagName("image_url").item(i)
						.getFirstChild().getNodeValue();
				String image_name = document.getElementsByTagName("image_name").item(i)
						.getFirstChild().getNodeValue();
				int image_size = Integer.parseInt(document.getElementsByTagName("image_size")
						.item(i).getFirstChild().getNodeValue());
				String season_sort = document.getElementsByTagName("season_sort").item(i)
						.getFirstChild().getNodeValue();
				
				values.put("season_id", season_id);
				values.put("season_title", season_title);
				values.put("season_count", season_count);
				values.put("season_info", season_info);
				values.put("pic_url", pic_url);
				values.put("pic_name", pic_name);
				values.put("pic_size", pic_size);
				values.put("image_url", image_url);
				values.put("image_name", image_name);
				values.put("image_size", image_size);
				values.put("pic_localUrl", getImageFolderPath() + pic_name);
				values.put("image_localUrl", getImageFolderPath() + image_name);
				values.put("season_sort", season_sort);
				sqliteDatabase.insert("season", null, values);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private String getImageFolderPath(){
		String SDCardRoot = Environment.getExternalStorageDirectory().getAbsolutePath();
		String path = SDCardRoot + File.separator + "MissPuff" + File.separator + "cache" + File.separator;
		return path;
	}
	
	private String getVideoFolderPath(){
		String SDCardRoot = Environment.getExternalStorageDirectory().getAbsolutePath();
		String path = SDCardRoot + File.separator + "MissPuff" + File.separator;
		return path;
	}
}
